package applet.attendee.unlocked.components.lodgings;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;
import data.account.attendee.lodging.LodgingData;

import applet.attendee.CMSAttendeeView;
import applet.misc.action.EnableActionListener;
import applet.misc.graphics.AttendeeBackgroundPanel;
import applet.misc.item.EnableItemListener;
import applet.misc.web.WebsiteActionListener;

public class CMSAttendeeLodgingsView extends AttendeeBackgroundPanel
{
	private static final long serialVersionUID = 6490614358660371039L;
	
	private CMSAttendeeView attendeeView;
	public CMSAttendeeView attendeeView() { return attendeeView; }

	private JPanel lodgingDisplay;
	private JPanel lodgingActiveDialog;
	private JPanel lodgingInactiveDialog;
	
	private JComboBox floor;
	private JComboBox room;
	private JComboBox dates;
	private JComboBox duration;
	private JComboBox mealPlan;
	private JLabel cost;
	private JButton saveButton;
	
	public CMSAttendeeLodgingsView(CMSAttendeeView attendeeView)
	{
		super(new GridBagLayout());
		this.attendeeView = attendeeView;
		setBorder(new EmptyBorder(25, 25, 25, 25));
		createComponents();
		
		JPanel lodgingDialog = new JPanel(new BorderLayout());
		lodgingDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		
			JPanel titlePane = new JPanel(new BorderLayout());
			titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
			titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel title = new JLabel("Lodgings Plan:");
				title.setFont(title.getFont().deriveFont(Font.BOLD));
				titlePane.add(title, BorderLayout.WEST);
			lodgingDialog.add(titlePane, BorderLayout.NORTH);
			
				lodgingActiveDialog = defineActiveDialog();
				lodgingInactiveDialog = defineInactiveDialog();
				
				lodgingDisplay = new JPanel(new BorderLayout());
				lodgingDisplay.setBorder(new BevelBorder(BevelBorder.LOWERED));
				
				if(attendeeView.attendee().lodging() != null)
				{
					System.out.println("Attendee has lodging data!");
					loadLodgingData(attendeeView().attendee().lodging());
					lodgingDisplay.add(lodgingActiveDialog, BorderLayout.CENTER);
				}
				else
				{
					System.out.println("Attendee lodging data is null!");
					lodgingDisplay.add(lodgingInactiveDialog, BorderLayout.CENTER);
				}
				lodgingDialog.add(lodgingDisplay, BorderLayout.CENTER);
			
			lodgingDialog.add(getThirdPartyBar(), BorderLayout.SOUTH);
		
		add(lodgingDialog);
	}
	
	private void createComponents()
	{
		saveButton = new JButton("Save Travel Plan");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.addActionListener(new SaveLodgingListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));
		saveButton.setEnabled(false);
		
		floor = new JComboBox(new String[] { "1", "2", "3", "4", "5" });
		floor.setPreferredSize(new Dimension(150, 30));
		floor.addItemListener(new EnableItemListener(saveButton, true));
		
		String[] roomList = new String[22];
		for(int i = 0; i < roomList.length; i++)
			roomList[i] = ((Integer) (i + 1)).toString();
		room = new JComboBox(roomList);
		room.setPreferredSize(new Dimension(150, 30));
		room.addItemListener(new EnableItemListener(saveButton, true));

		dates = new JComboBox(new String[] { "10 - 01 - 12", "10 - 02 - 12", "10 - 03 - 12",
											"10 - 04 - 12", "10 - 05 - 12", "10 - 06 - 12",
											"10 - 07 - 12", "10 - 08 - 12" });
		dates.setPreferredSize(new Dimension(150, 30));
		dates.addItemListener(new EnableItemListener(saveButton, true));
		
		duration = new JComboBox(new String[] { "1", "2", "3", "4", "5", "6", "7" });
		duration.setPreferredSize(new Dimension(150, 30));
		duration.addItemListener(new EnableItemListener(saveButton, true));
		duration.addItemListener(new CostListener());

		mealPlan = new JComboBox(new String[] { "None", "Continental", "Full" });
		mealPlan.setPreferredSize(new Dimension(150, 30));
		mealPlan.addItemListener(new EnableItemListener(saveButton, true));
		mealPlan.addItemListener(new CostListener());
		
		cost = new JLabel("Cost: $120.00");
	}
	
	private JPanel defineActiveDialog()
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setPreferredSize(new Dimension(450, 400));
	
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Holiday Inn Reservations:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.WEST);
			
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Floor:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);

		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(floor, BorderLayout.WEST);

		panel.add(layoutHelper);
			
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Room:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(room, BorderLayout.WEST);
	
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Date to arrive:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(dates, BorderLayout.WEST);
	
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Stay Duration:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(duration, BorderLayout.WEST);
	
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Meal Plan:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(mealPlan, BorderLayout.WEST);
	
		panel.add(layoutHelper);
		
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(cost, BorderLayout.WEST);
	
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new GridBagLayout());
			layoutHelper.add(saveButton);

		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new GridBagLayout());
				JButton deleteButton = new JButton("Delete Travel Plan");
				deleteButton.setPreferredSize(new Dimension(200, 30));
				deleteButton.setFont(deleteButton.getFont().deriveFont(Font.BOLD));
				deleteButton.addActionListener(new DeleteLodgingListener());
			layoutHelper.add(deleteButton);

		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel defineInactiveDialog()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setPreferredSize(new Dimension(450, 400));
			
			JButton createTravel = new JButton("Create Lodging Plan");
			createTravel.setPreferredSize(new Dimension(200, 30));
			createTravel.setFont(createTravel.getFont().deriveFont(Font.BOLD));
			createTravel.addActionListener(new CreateLodgingListener());
			createTravel.addActionListener(new EnableActionListener(saveButton, true));
			
		panel.add(createTravel);
		return panel;
	}
	
	private JPanel getThirdPartyBar()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
			JButton thirdPartyButton = new JButton("Find Hotels Online");
			thirdPartyButton.setPreferredSize(new Dimension(200, 30));
			thirdPartyButton.addActionListener(new WebsiteActionListener("http://www.hotels.com/"));
		panel.add(thirdPartyButton, BorderLayout.EAST);
		return panel;
	}
	
	private void loadLodgingData(LodgingData data)
	{
		if(data != null)
		{
			for(int i = 0; i < floor.getItemCount(); i++)
				if(Integer.parseInt((String) floor.getItemAt(i)) == data.floor())
					floor.setSelectedIndex(i);
			
			for(int i = 0; i < room.getItemCount(); i++)
				if(Integer.parseInt((String) room.getItemAt(i)) == data.room())
					room.setSelectedIndex(i);

			for(int i = 0; i < dates.getItemCount(); i++)
				if(dates.getItemAt(i).equals(data.startDate()))
					dates.setSelectedIndex(i);
			
			for(int i = 0; i < duration.getItemCount(); i++)
				if(Integer.parseInt((String) duration.getItemAt(i)) == data.duration())
					duration.setSelectedIndex(i);

			for(int i = 0; i < mealPlan.getItemCount(); i++)
				if(mealPlan.getItemAt(i).equals(data.mealPlan()))
					mealPlan.setSelectedIndex(i);
			
			cost.setText("Cost: $" + data.cost());
		}
	}
	
	private LodgingData createLodgingData()
	{
		int floor = Integer.parseInt((String) this.floor.getSelectedItem());
		int room = Integer.parseInt((String) this.room.getSelectedItem());
		int duration = Integer.parseInt((String) this.duration.getSelectedItem());
		String startDate = (String) this.dates.getSelectedItem();
		String mealPlan = (String) this.mealPlan.getSelectedItem();
		int cost = this.mealPlan.getSelectedIndex();
		cost *= cost * 5 * duration;
		cost += (duration * 120);
		return new LodgingData(floor, room, startDate, duration, mealPlan, cost);
	}
	
	private void updateCost()
	{
		int duration = Integer.parseInt((String) this.duration.getSelectedItem());
		int cost = this.mealPlan.getSelectedIndex();
		cost *= (cost * 5 * duration);
		cost += (duration * 120);
		this.cost.setText("Cost: $" + cost + ".00");
	}
	
	private class CreateLodgingListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			loadLodgingData(attendeeView().attendee().lodging());
			lodgingDisplay.remove(lodgingInactiveDialog);
			lodgingDisplay.add(lodgingActiveDialog, BorderLayout.CENTER);
			lodgingDisplay.validate();
			lodgingDisplay.repaint();
		}
	}
	
	private class DeleteLodgingListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			attendeeView.attendee().setLodging(null);
			attendeeView.attendee().saveData();
			lodgingDisplay.remove(lodgingActiveDialog);
			lodgingDisplay.add(lodgingInactiveDialog, BorderLayout.CENTER);
			lodgingDisplay.validate();
			lodgingDisplay.repaint();
		}
	}
	
	private class CostListener implements ItemListener {
		@Override public void itemStateChanged(ItemEvent e) { updateCost(); } }
	
	private class SaveLodgingListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			attendeeView().attendee().setLodging(createLodgingData());
			attendeeView().attendee().saveData();
		}
	}
}